<?php

/*
 * 又拍云存储后台管理列表，包括用户上传文件的列表和搜索.
 * 
 * @author Kun Xu <kunx@jumei.com>
 */

/**
 * Description of UpyunadminController
 *
 * @author 坤
 */
class UpyunadminController extends Controller
{

    public $navbarTitle = '用户上传后台管理';
    public $layout = '//layouts/column2';
//    public $menu = array();
    public $menu;

    public function beforeAction()
    {
        $this->menu = array(
            array('label' => '前台'),
            array('label' => '上传文件', 'icon' => 'icon-arrow-up', 'url' => Yii::app()->createUrl('upyun/forms')),
//            array('label' => 'Settings', 'icon' => 'cog', 'url' => '#'),
//            array('label' => 'Help', 'icon' => 'flag', 'url' => '#'),
            '-----',
            array('label' => '后台'),
            array('label' => '文件列表', 'icon' => 'cog', 'url' => Yii::app()->createUrl($this->module->id . '/' . $this->id . '/list'), 'active' => true),
//            array('label' => 'Library', 'icon' => 'book', 'url' => '#'),
//            array('label' => 'Application', 'icon' => 'pencil', 'url' => '#'),
        );
        return true;
    }

    public function filters()
    {
        return array('accessControl');
    }

    public function accessRules()
    {
        return array(
//            array(
//                'deny',
//                'actions' => array('delete'),
//                'users' => array('?'),
////                'users' => array('*'),
//            ),
            array(
                'allow',
                'actions' => array('list', 'delete', 'show', 'update'),
//                'users' => array('isAdmin'),
                'expression' => array($this, 'isAdmin'), //非admin用户禁止删除
//                'users' => array('*'),
            ),
            array(
                'deny',
                'users' => array('*'),
            ),
        );
    }

    /**
     * 判断是否是admin
     * @return boolean
     */
    public function isAdmin()
    {
        if (Yii::app()->user->getState('role') === 'admin') {
            return true;
        } else {
            return false;
        }
    }

    public function actionIndex()
    {
        $model = new UserUploadFile;
        if (isset($_POST['UserUploadFile'])) {
            $model->attributes = $_POST['UserUploadFile'];
        }
        $form = new CForm($model->getSearchFormConfig(), $model);
        var_dump($model);
        $data = array(
            'form' => $form,
        );
        Yii::app()->name = 'UpYun管理后台';
        $this->render('index', $data);
    }

    //展示单条数据
    public function actionShow($id = 1)
    {
        $id = trim($id);
        $model = UserUploadFile::model()->findByPk($id);
        $data = array(
            'model' => $model,
        );
        $this->render('show', $data);
    }

    /**
     * 删除指定记录
     * @param string $id
     */
    public function actionDelete($id)
    {
        $id = trim($id);
        $model = UserUploadFile::model()->findByPk($id);
        if ($model) {
            if ($model->delete()) {
                echo '删除' . $id . '成功';
            } else {
                var_dump($model->getErrors());
            }
        } else {
            echo '查无此记录';
            sleep(3);
            $url = Yii::app()->createUrl($this->module->id . '/' . $this->id . '/list', array());
            $this->redirect($url);
        }
    }

    /**
     * 修改记录，其实并没有执行修改操作
     * @param type $id
     */
    public function actionUpdate($id)
    {
        $id = trim($id);
        $model = UserUploadFile::model()->findByPk($id);

        if (isset($_POST['UserUploadFile'])) {
            $model->attributes = $_POST['UserUploadFile'];
            //echo '你不会想改变这条记录的！';
        }
        if ($model) {
            $model->upload_time = date('Y-m-d H:i:s', $model->upload_time);
            $this->render('update', array('model' => $model));
        } else {
            echo '查无此记录';
            sleep(3);
            $url = Yii::app()->createUrl($this->module->id . '/' . $this->id . '/list', array());
            $this->redirect($url);
        }
    }

    /**
     * 管理列表
     */
    public function actionList()
    {
//        Yii::import('bootstrap.form.TbForm');
//        var_dump(Yii::getPathOfAlias('bootstrap'));
//        echo Yii::app()->getRequest()->getUserAgent();
//        $model = new UserUploadFile(); //实例化模型
        $model = new UserUploadFileFM(); //实例化模型
//        if (isset($_GET['UserUploadFile'])) {
//            $model->attributes = $_GET['UserUploadFile'];
//        }
//        $form = new CForm($model->getSearchFormConfig(), $model); //渲染表单
        $form = new BsForm($model->getSearchFormConfig(), $model); //渲染表单
        $form->submitted();
        $criteria = new CDbCriteria(); //构建查询器
        if ($model->user_id) {
            $criteria->addCondition('t.user_id=:user_id');
            $criteria->params[':user_id'] = $model->user_id;
        }
        if ($model->bucket) {
            $criteria->addSearchCondition('t.bucket', $model->bucket);
        }
        $criteria->order = 't.id ASC';


        $dataProvider = new CActiveDataProvider('UserUploadFile', array(
            'criteria' => $criteria,
            'pagination' => array(
                'pageSize' => 20,
            ),
        ));
        $data = array(
            'dataProvider' => $dataProvider,
            'form' => $form,
        );
        $this->render('list', $data);
    }

}
